﻿@namespace MudBlazor.UnitTests.TestComponents

<MudTable Items="@Elements" Hover="true" Breakpoint="Breakpoint.Sm" Loading="@_loading" Filter="new Func<Element, bool>(FilterFunc)">
    <ToolBarContent>
        <MudTextField id="searchString" @bind-Value="searchString" Placeholder="Search"></MudTextField>
    </ToolBarContent>
    <HeaderContent>
        <MudTh>Nr</MudTh>
        <MudTh>Sign</MudTh>
        <MudTh>Name</MudTh>
        <MudTh>Position</MudTh>
        <MudTh>Molar mass</MudTh>
    </HeaderContent>
    <RowTemplate>
        <MudTd DataLabel="Nr">@context.Number</MudTd>
        <MudTd DataLabel="Sign">@context.Sign</MudTd>
        <MudTd DataLabel="Name">@context.Name</MudTd>
        <MudTd DataLabel="Position">@context.Position</MudTd>
        <MudTd DataLabel="Molar mass">@context.Molar</MudTd>
    </RowTemplate>
    <NoRecordsContent>No matching records found</NoRecordsContent>
    <LoadingContent>Loading...</LoadingContent>
</MudTable>

<MudSwitch id="switch" @bind-Checked="_loading">Show Loading</MudSwitch>

@code {
    public static string __description__ = "When loading is true a progress bar should appear";

    private bool _loading = false;
    private IEnumerable<Element> Elements = new List<Element>();
    private string searchString;

    protected override void OnInitialized()
    {
        Elements = new List<Element>()
        {
            new Element() { Molar = "A", Sign = "A", Name = "A", Number = "A", Position = "A"},
            new Element() { Molar = "B", Sign = "B", Name = "B", Number = "B", Position = "B"},
        };
    }

    private bool FilterFunc(Element element)
    {
        if (string.IsNullOrWhiteSpace(searchString))
            return true;
        if (element.Number.Contains(searchString, StringComparison.OrdinalIgnoreCase))
            return true;
        if (element.Sign.Contains(searchString, StringComparison.OrdinalIgnoreCase))
            return true;
        if (element.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase))
            return true;
        if (element.Position.Contains(searchString, StringComparison.OrdinalIgnoreCase))
            return true;
        if (element.Molar.Contains(searchString, StringComparison.OrdinalIgnoreCase))
            return true;
        return false;
    }

    public class Element
    {
        public string Number { get; set; }
        public string Sign { get; set; }
        public string Name { get; set; }
        public string Position { get; set; }
        public string Molar { get; set; }
    }
}